package com.dy.数组.初级.有效的数独;

public class Solution {
    public static boolean isValidSudoku(char[][] board) {
        //记录行
        boolean row[][] = new boolean[9][10];
        //记录列
        boolean col[][] = new boolean[9][10];
        //记录宫
        boolean block[][] = new boolean[9][10];

        for(int i = 0; i<9; i++){
            for(int j = 0; j<9; j++){
                if(board[i][j]!='.') {
                    int num = board[i][j] - '0';
                    if (row[i][num] || col[j][num] || block[i / 3 * 3 + j / 3][num])
                        return false;
                    else {
                        row[i][num] = true;
                        col[j][num] = true;
                        block[i / 3 * 3 + j / 3][num] = true;
                    }
                }
            }
        }
        return true;

    }

    public boolean isValidSudoku2(char[][] board) {
        for(int i = 0; i < 9; i++){
            for(int j = 0; j < 9; j++){
                if(board[i][j] == '.')continue;
                for(int k = 8; k > j; k-- )
                    if(board[i][j] == board[i][k])
                        return false;
                for(int k = 8; k > i; k--)
                    if(board[i][j] == board[k][j])
                        return false;
                for(int k = i + 1; k % 3 != 0; k ++){
                    for(int h = j /3 *3;h < j / 3 *3 + 3; h ++ )
                        if(board[i][j] == board[k][h])
                            return false;
                }
            }
        }
        return true;
    }
}
